package www.lagou.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import www.lagou.entity.Phone;
import www.lagou.utils.DateUtils;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;
import java.util.List;

public class PhoneDao {



    /**
     * 通过 price 查找 价格大于 price 的 并且 通过 date 查找 生产日期 小于 date 的 所有手机
     *
     * @param price     大于 price 的数据
     * @param date      小于 date 的数据
     * @return
     * @throws SQLException
     */
    public List<Phone> findPhoneByPriceAndDate(double price, String date) throws SQLException {

        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "select * from phone where price > ? and prodate < ? ";

        Object[] params = {price,date};

        List<Phone> query = qr.query(sql, new BeanListHandler<>(Phone.class), params);

        return query;
    }

    // 通过 color 查找 颜色是 color 的手机信息
    public List<Phone> findPhoneByColor(String color) throws SQLException {

        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "select * from phone where color = ?";

        Object[] params = {color};

        List<Phone> query = qr.query(sql, new BeanListHandler<>(Phone.class), params);

        return query;
    }
}
